﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;

namespace Kokomo
{
	public static class DragDropHelper
	{
		#region DropBehavior property
		public static DropBehavior GetDropBehavior(DependencyObject obj)
		{
			return (DropBehavior)obj.GetValue(DropBehaviorProperty);
		}

		public static void SetDropBehavior(DependencyObject obj, DropBehavior value)
		{
			obj.SetValue(DropBehaviorProperty, value);
		}

		// Using a DependencyProperty as the backing store for DropBehavior.  This enables animation, styling, binding, etc...
		public static readonly DependencyProperty DropBehaviorProperty =
			DependencyProperty.RegisterAttached("DropBehavior", typeof(DropBehavior), typeof(DragDropHelper), new UIPropertyMetadata(null, OnDropBehaviorChanged));

		private static void OnDropBehaviorChanged(DependencyObject target, DependencyPropertyChangedEventArgs e)
		{
			FrameworkElement element = target as FrameworkElement;
			if (element != null)
			{
				DropBehavior oldBehavior = (DropBehavior)e.OldValue;

				DropBehavior newBehavior = (DropBehavior)e.NewValue;
				if (newBehavior != null)
				{
					element.AllowDrop = true;
					element.DragOver += new DragEventHandler(newBehavior.Element_DragOver);
					element.Drop += new DragEventHandler(newBehavior.Element_Drop);
				}
			}
		}
		#endregion

		#region DragBehavior property


		public static DragBehavior GetDragBehavior(DependencyObject obj)
		{
			return (DragBehavior)obj.GetValue(DragBehaviorProperty);
		}

		public static void SetDragBehavior(DependencyObject obj, DragBehavior value)
		{
			obj.SetValue(DragBehaviorProperty, value);
		}

		// Using a DependencyProperty as the backing store for DragBehavior.  This enables animation, styling, binding, etc...
		public static readonly DependencyProperty DragBehaviorProperty =
			DependencyProperty.RegisterAttached("DragBehavior", typeof(DragBehavior), typeof(DragDropHelper), new UIPropertyMetadata(null, OnDragBehaviorChanged));

		private static void OnDragBehaviorChanged(DependencyObject target, DependencyPropertyChangedEventArgs e)
		{
			FrameworkElement element = target as FrameworkElement;
			if (element != null)
			{
				DragBehavior oldBehavior = (DragBehavior)e.OldValue;

				DragBehavior newBehavior = (DragBehavior)e.NewValue;
				if (newBehavior != null)
				{
					DragHelper helper = GetDragHelper(element);
					if (helper == null)
					{
						helper = new DragHelper(element);
						SetDragHelper(element, helper);
					}

					element.QueryContinueDrag += new QueryContinueDragEventHandler(newBehavior.Element_QueryContinueDrag);
				}
			}
		}


		#endregion

		#region DragHelper property


		private static DragHelper GetDragHelper(DependencyObject obj)
		{
			return (DragHelper)obj.GetValue(DragHelperProperty);
		}

		private static void SetDragHelper(DependencyObject obj, DragHelper value)
		{
			obj.SetValue(DragHelperProperty, value);
		}

		// Using a DependencyProperty as the backing store for DragHelper.  This enables animation, styling, binding, etc...
		private static readonly DependencyProperty DragHelperProperty =
			DependencyProperty.RegisterAttached("DragHelper", typeof(DragHelper), typeof(DragDropHelper), new UIPropertyMetadata(null));


		#endregion
	}
}
